<template>
  <div>
    <a-form @submit="handleSubmit" :form="form" style="width:40%;;margin:0 auto;">
      <a-row>
        <a-col :xs="{ span : 24 }" :sm="{ span:7 }" class="formGroupTitle">重置登录密码</a-col>
      </a-row>
      <a-form-item v-bind="formItemLayout" label="新登录密码" help="登录密码长度不低于8位，且至少包含数字、大小写字母、特殊字符中的两种">
        <a-input
          placeholder="请输入新登录密码"
          v-decorator="[
                        'loginpwd',
                        {
                            rules: [{
                            required: true, message: '请输入新登录密码',
                            },{ validator: validateToNextPassword }
                            ],validateTrigger: 'blur'
                        }
                        ]"
          type="password"
        />
      </a-form-item>
      <a-form-item v-bind="formItemLayout" label="确认新登录密码">
        <a-input
          placeholder="确认新登录密码"
          v-decorator="[
                        'loginpwd2',
                        {
                            rules: [{
                            required: true, message: '确认新登录密码',
                            }, {
                            validator: compareToFirstPassword,
                            }],validateTrigger: 'blur'
                        }
                        ]"
          type="password"
        />
      </a-form-item>
      <div style="text-align:center;">
        <a-button size="large" htmlType="submit" type="primary">下一步</a-button>
      </div>
    </a-form>
    <a-modal title="登录密码重置成功！" v-model="visible" :footer="null">
      <div style="text-align:center;">
        <a-button type="primary" @click="gologin">重新登录</a-button>
      </div>
    </a-modal>
  </div>
</template>

<script>
import AForm from "ant-design-vue/es/form/Form";
import ATabs from "ant-design-vue/es/tabs";
import GlobalFooter from "../../layouts/GlobalFooter";
import AFormItem from "ant-design-vue/es/form/FormItem";
import AInput from "ant-design-vue/es/input/Input";
import AIcon from "ant-design-vue/es/icon";
import AInputGroup from "ant-design-vue/es/input/Group";
import AButton from "ant-design-vue/es/button/button";
import ACol from "ant-design-vue/es/grid/Col";
import ARow from "ant-design-vue/es/grid/Row";
import ACheckbox from "ant-design-vue/es/checkbox/Checkbox";
import AAlert from "ant-design-vue/es/alert/index";

const ATabPane = ATabs.TabPane;

export default {
  name: "Login",
  components: {
    AAlert,
    ACheckbox,
    ACol,
    ARow,
    AButton,
    AInputGroup,
    AIcon,
    AInput,
    AFormItem,
    GlobalFooter,
    ATabPane,
    ATabs,
    AForm
  },
  data() {
    return {
      visible: false,
      form: this.$form.createForm(this),
      error: "",
      loginpwd:'',
      loginpwd2:'',
      login: false,
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 12 },
          md: { span: 10 }
        }
      }
    };
  },
  watch: {},
  computed: {},
  mounted() {},
  methods: {
    handleChange(value) {
      console.log(value);
    },
    handleSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (!err) {
          this.visible = true;
          console.log("Received values of form: ", values);
          //   this.codeConfirm();
        }
      });
    },
    // 登录密码验证
    validateToNextPassword(rule, value, callback) {
      const form = this.form;
      if (
        value &&
        !/(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*?]+)$)^[\w~!@#$%^&*?.]{8,30}$/.test(
          value
        )
      ) {
        // form.validateFields(['confirm'], { force: true });
        callback("您输入密码的格式有误！");
      } else {
        console.log("loginpwd", value);
        this.loginpwd = value;
      }
      callback();
    },
    compareToFirstPassword(rule, value, callback) {
      const form = this.form;
      if (value && value !== form.getFieldValue("loginpwd")) {
        callback("两次密码不一致");
      } else {
        callback();
      }
    },
    handleOk() {
      this.$router.push("/home");
    },
    gologin() {
      this.visible = false;
      this.$router.push("/home");
    },
  }
};
</script>

<style lang="less" scoped>
span {
  cursor: pointer;
}
</style>
